package cn.niit.letuseatdemo.config;


import cn.niit.letuseatdemo.interceptor.AuthInterceptor;
import cn.niit.letuseatdemo.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
* @Description: ${TODO}
* @Author: Jin_Na
* @CreateTime: 2022/10/24 23:34
 * @param: null
 * @return: null
**/
@Slf4j
@Configuration
public class MyWebMVCConfigurer implements WebMvcConfigurer {
    @Value("${springmvc.interceptor.exclude-path-patterns}")
    private List<String> EXCLUDE_PATH_PATTERNS;
    @Autowired
    private LoginInterceptor loginInterceptor;

    //前端登录拦截器
    @Autowired
    private AuthInterceptor authInterceptor;

    /**
     * @Description: 注册拦截器
     * @Author: Jin_Na
     * @CreateTime: 2022/10/11 8:56
     * @param: registry
     **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration =
                registry.addInterceptor(loginInterceptor);
        //添加一个拦截策略，拦截所有请求；
        registration.addPathPatterns("/**");
        //配置拦截白名单，不用拦截，直接放行；

        log.info("MyWebMVCConfigurer.addInterceptors 登录拦截器白名单：{}",
                EXCLUDE_PATH_PATTERNS);

        registration.excludePathPatterns(EXCLUDE_PATH_PATTERNS);//不对/admin/login /store/login请求进行拦截
        InterceptorRegistration authRegistration = registry.addInterceptor(authInterceptor);
        authRegistration.addPathPatterns("/api/**");    //拦截所有请求；
        //配置拦截白名单：对公共地址不进行拦截
        authRegistration.excludePathPatterns("/api/user/register",
                "/api/user/kaptcha", "/api/user/smscode", "/api/user/login",
                "/api/store/*","/api/store/categoryshop/*","/api/store",
                "/api/store/goods/*","/api/store/goods/classify/**",
                "/api/store/goods/classify/*","/api/ratingadd/*","/api/rating/*","/api/order/**");
    }

    @Value("${spring.mvc.allowed.origins}")
    private String[] allowedOriginPatterns;


    /*
    * @Description: 永久解决跨域
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/11/15 13:23
     * @param: registry
    **/
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
                .allowCredentials(true)/* allowCredentials 是否发送 Cookie */
                .allowedOrigins(allowedOriginPatterns) /* 放行那些原始域，allowedOrigins 不可以包含 "*" */
                //.allowedOriginPatterns(allowedOriginPatterns.toArray())
                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
                .allowedHeaders("*")
                .exposedHeaders("*");
    }
}
